/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import org.aergia.vinny.core.enums.DayOfWeek;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.gui.util.LabelPanel;

public class InitializeLocation extends JDialog implements ActionListener {

	private static final String END_TIME = "6:00 PM";

	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";

	private static final long serialVersionUID = 1L;

	private static final String START_TIME = "7:00 AM";

	/**
	 * Main entry of the class. Note: This class is only created so that you can
	 * easily preview the result at runtime. It is not expected to be managed by
	 * the designer. You can modify it as you like.
	 */
	public static void main(final String[] args) {
		InitializeLocation.installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				final InitializeLocation dialog = new InitializeLocation();
				dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				dialog.setTitle("InitializeLocation");
				dialog.setLocationRelativeTo(null);
				dialog.getContentPane().setPreferredSize(dialog.getSize());
				dialog.pack();
				dialog.setVisible(true);
			}
		});
	}

	private static void installLnF() {
		try {
			String lnfClassname = InitializeLocation.PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null) {
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			}
			UIManager.setLookAndFeel(lnfClassname);
		} catch (final Exception e) {
			Logger.getAnonymousLogger().severe("Cannot install " + InitializeLocation.PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage());
		}
	}

	private JPanel jPanel0;
	private JScrollPane jScrollPane0;
	private JTextField locText;
	private JTable operatingHours;
	private JTextField orgText;
	private JTextField rateText;

	private JButton saveButton;

	public InitializeLocation() {
		this.initComponents();
	}

	public InitializeLocation(final Dialog parent) {
		super(parent);
		this.initComponents();
	}

	public InitializeLocation(final Dialog parent, final boolean modal) {
		super(parent, modal);
		this.initComponents();
	}

	public InitializeLocation(final Dialog parent, final String title) {
		super(parent, title);
		this.initComponents();
	}

	public InitializeLocation(final Dialog parent, final String title, final boolean modal) {
		super(parent, title, modal);
		this.initComponents();
	}

	public InitializeLocation(final Dialog parent, final String title, final boolean modal, final GraphicsConfiguration arg) {
		super(parent, title, modal, arg);
		this.initComponents();
	}

	public InitializeLocation(final Frame parent) {
		super(parent);
		this.initComponents();
	}

	public InitializeLocation(final Frame parent, final boolean modal) {
		super(parent, modal);
		this.initComponents();
	}

	public InitializeLocation(final Frame parent, final String title) {
		super(parent, title);
		this.initComponents();
	}

	public InitializeLocation(final Frame parent, final String title, final boolean modal) {
		super(parent, title, modal);
		this.initComponents();
	}

	public InitializeLocation(final Frame parent, final String title, final boolean modal, final GraphicsConfiguration arg) {
		super(parent, title, modal, arg);
		this.initComponents();
	}

	public InitializeLocation(final Window parent) {
		super(parent);
		this.initComponents();
	}

	public InitializeLocation(final Window parent, final ModalityType modalityType) {
		super(parent, modalityType);
		this.initComponents();
	}

	public InitializeLocation(final Window parent, final String title) {
		super(parent, title);
		this.initComponents();
	}

	public InitializeLocation(final Window parent, final String title, final ModalityType modalityType) {
		super(parent, title, modalityType);
		this.initComponents();
	}

	public InitializeLocation(final Window parent, final String title, final ModalityType modalityType, final GraphicsConfiguration arg) {
		super(parent, title, modalityType, arg);
		this.initComponents();
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(final ActionEvent e) {
		if (e.getSource() == this.saveButton) {
			this.setVisible(false);
		}

	}

	public int getEndTime(final DayOfWeek day) {
		final String value = (String) this.getOperatingHours().getModel().getValueAt(day.getValue() - 1, 2);
		if (!value.isEmpty()) {
			return this.getHour(value);
		}
		return 0;
	}

	/**
	 * @return the location name
	 */
	public String getLocationName() {
		return this.locText.getText();
	}

	/**
	 * @return the organization name
	 */
	public String getOrganizationName() {
		return this.orgText.getText();
	}

	public int getRate() {
		return 2085;
	}

	public int getStartTime(final DayOfWeek day) {
		final String value = (String) this.getOperatingHours().getModel().getValueAt(day.getValue() - 1, 1);
		if (!value.isEmpty()) {
			return this.getHour(value);
		}
		return 0;
	}

	public boolean isDayDefined(final DayOfWeek day) {
		final String string = (String) this.operatingHours.getModel().getValueAt(day.getValue() - 1, 1);
		return !string.isEmpty();
	}

	private int getHour(final String value) {
		final Pattern pattern = Pattern.compile("(\\d+):(\\d+) (.*)");
		final Matcher matcher = pattern.matcher(value);
		if (matcher.find()) {
			try {
				int hour = Integer.parseInt(matcher.group(1));
				final int minute = Integer.parseInt(matcher.group(2));
				if (matcher.group(3).startsWith("P")) {
					hour += 12;
				}
				return hour * 60 + minute;
			} catch (final Exception e) {
				// ignore;
			}
		}
		return 0;
	}

	private JPanel getJPanel0() {
		if (this.jPanel0 == null) {
			this.jPanel0 = new JPanel();
			this.jPanel0.setBorder(BorderFactory.createTitledBorder(new LineBorder(Color.black, 1, false), "Operating Hours", TitledBorder.LEADING,
					TitledBorder.DEFAULT_POSITION, new Font("Dialog", Font.BOLD, 12), new Color(51, 51, 51)));
			this.jPanel0.setLayout(new BorderLayout());
			this.jPanel0.add(this.getJScrollPane0(), BorderLayout.CENTER);
		}
		return this.jPanel0;
	}

	private JScrollPane getJScrollPane0() {
		if (this.jScrollPane0 == null) {
			this.jScrollPane0 = new JScrollPane();
			this.jScrollPane0.setViewportView(this.getOperatingHours());
		}
		return this.jScrollPane0;
	}

	private JTextField getLocText() {
		if (this.locText == null) {
			this.locText = new JTextField("Resource Center");
			this.locText.setName("Location Name");
			this.locText.setToolTipText("Name of this location.");
		}
		return this.locText;
	}

	private JTable getOperatingHours() {
		if (this.operatingHours == null) {
			this.operatingHours = new JTable();
			this.operatingHours.setModel(new DefaultTableModel(new Object[][] { { "Sunday", "", "" },
					{ "Monday", InitializeLocation.START_TIME, InitializeLocation.END_TIME },
					{ "Tuesday", InitializeLocation.START_TIME, InitializeLocation.END_TIME },
					{ "Wednesday", InitializeLocation.START_TIME, InitializeLocation.END_TIME },
					{ "Thursday", InitializeLocation.START_TIME, InitializeLocation.END_TIME },
					{ "Friday", InitializeLocation.START_TIME, InitializeLocation.END_TIME }, { "Saturday", "", "" }, }, new String[] { "Day of Week",
					"Start Time", "End Time" }) {
				private static final long serialVersionUID = 1L;
				private final Class<?>[] types = new Class<?>[] { Object.class, Object.class, Object.class };

				@Override
				public Class<?> getColumnClass(final int columnIndex) {
					return this.types[columnIndex];
				}
			});
		}
		return this.operatingHours;
	}

	private JTextField getOrgText() {
		if (this.orgText == null) {
			this.orgText = new JTextField("City of Reno");
			this.orgText.setName("Organization Name");
			this.orgText.setToolTipText("Name of this location's organization.");
		}
		return this.orgText;
	}

	private JTextField getRateText() {
		if (this.rateText == null) {
			this.rateText = new JTextField();
			this.rateText.setText("$20.85");
			this.rateText.setName("Volunteer Rate");
			this.rateText.setToolTipText("Value of volunteer time");
		}
		return this.rateText;
	}

	private JButton getSaveButton() {
		if (this.saveButton == null) {
			this.saveButton = new JButton();
			this.saveButton.setText("Save");
			this.saveButton.addActionListener(this);
		}
		return this.saveButton;
	}

	private void initComponents() {
		this.setTitle("Initialize Database");
		this.setFont(new Font("Dialog", Font.PLAIN, 12));
		this.setBackground(Color.white);
		this.setModal(true);
		this.setForeground(Color.black);
		this.setLayout(new BorderLayout());

		final JPanel options = new LabelPanel(FontUtil.getLabelFont(), Color.BLACK, this.getOrgText(), this.getLocText(), this.getRateText());

		final JPanel panel1 = new JPanel(new BorderLayout());
		panel1.add(options, BorderLayout.NORTH);
		panel1.add(this.getJPanel0(), BorderLayout.SOUTH);
		this.add(panel1, BorderLayout.NORTH);

		final JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
		buttonPanel.add(new JLabel());
		buttonPanel.add(new JLabel());
		buttonPanel.add(this.getSaveButton());
		this.add(buttonPanel, BorderLayout.SOUTH);
		this.pack();
	}

}
